package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.UserBankMapper;
import com.bsh.mybatis.model.UserBank;

@Repository
public class DaoUserBank {
	@Resource
	SqlSessionTemplate session;

	UserBankMapper getUserBankMapper() {
		return session.getMapper(UserBankMapper.class);
	}

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;

	UserBankMapper getMapper() {
		return session.getMapper(UserBankMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(UserBank record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(UserBank record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}
	
	/**
	 * 查询用户的某张银行卡
	 * @param record
	 * @return
	 */	
	public UserBank selectUserOneBankCard(Integer bankCardId,Integer userId) {
		UserBank row = getMapper().selectUserOneBankCard(bankCardId,userId);
		return row;
	}
	
	public List<Map<String, Object>> selectByBankCard(String bankCard) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" SELECT t1.*  FROM user_bank t1 ");
		sb.append("WHERE t1.status=1 and t1.bank_card = " +"'" +bankCard+"'");
		// 
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(
				sb.toString(), ps);
		return list;
	}

	public int updateByPrimaryKeySelective(UserBank record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	/**
	 * 添加用户银行
	 * 
	 * @param userid
	 * @return
	 */
	public Integer addUserBank(UserBank ub) {
		UserBankMapper map = session.getMapper(UserBankMapper.class);
		return map.insert(ub);
	}

	public int updateByPrimaryKey(UserBank record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	/**
	 * 查询当前用户的银行卡列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> queryMyBankList(Integer userid) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" SELECT t1.is_merchant, t1.id, t1.bank_card, t1.identify_name as real_name, ");
		sb.append(" t2.name as bank_name, t2.icon as img FROM user_bank t1 LEFT JOIN bank t2 on t1.bankid = t2.id ");
		sb.append(" WHERE t1.status=1 AND t1.userid = " + userid);

		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(
				sb.toString(), ps);

		return list;
	}

	/**
	 * 查询当前用户的银行卡列表
	 * 
	 * @return
	 */
	public Integer queryMyCardsNumber(Integer userid,Integer isMerchant) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" SELECT t1.is_merchant, t1.id, t1.bank_card, t1.identify_name as real_name, ");
		sb.append(" t2.name as bank_name, t2.icon as img FROM user_bank t1 LEFT JOIN bank t2 on t1.bankid = t2.id ");
		sb.append(" WHERE t1.status=1 AND t1.is_merchant='"+isMerchant+"' AND t1.userid = " + userid);

		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(
				sb.toString(), ps);

		return list.size();
	}
}
